<?php
namespace Auntie\Controller;
use Think\Controller;
class IndexController extends BaseController{
//class IndexController extends Controller{
		
	public static $session;		//SESSION
	
	protected function _initialize(){
		parent::_initialize();
		
		header("Content-Type: text/html;charset=utf-8");
		$this->assign('tj',time());
		self::$session=$_SESSION['auntie'];		//session
		
		//p(self::$session);
		$this->assign('session',self::$session);
		$this->assign('entry',self::$entry);
	}

    public function index(){
		
		$ordernum=M('bb_order')->where(array('auntieid'=>self::$session['auntie_user']['id'],'status'=>array('in','1,2,3')))->count();
		$this->assign('dsouordernum',$ordernum[0]['count']);
		
		$ordernum=M('bb_order')->where(array('auntieid'=>self::$session['auntie_user']['id'],'status'=>'5'))->count();
		$this->assign('dsonordernum',$ordernum[0]['count']);
		$this->display();
    }
	//一抢单
	public function order01(){
       	$order=M('bb_order')->where(array('auntieid'=>self::$session['auntie_user']['id'],'status'=>array('in','1,2,3')))->order('addtime desc')->select();
		
		foreach ($order as $k => $v) {
			$uimg=M('weixin_client_user')->field('headimgurl')->where(array('openid'=>$v['openid']))->find();
			$order[$k]['uimg']=$uimg['headimgurl'];
		}
		$this->assign('order',$order);
		$this->display();
	}
	
	public function order02(){
       	$order=M('bb_order')->where(array('auntieid'=>self::$session['auntie_user']['id'],'status'=>'5'))->order('addtime desc')->select();

		foreach ($order as $k => $v) {
			$uimg=M('weixin_client_user')->field('headimgurl')->where(array('openid'=>$v['openid']))->find();
			$order[$k]['uimg']=$uimg['headimgurl'];
		}
		$this->assign('order',$order);
		$this->display();
	}
	
	public function order03(){
       	$order=M('bb_order')->where(array('auntieid'=>self::$session['auntie_user']['id'],'status'=>'6'))->order('addtime desc')->select();
		foreach ($order as $k => $v) {
			$uimg=M('weixin_client_user')->field('headimgurl')->where(array('openid'=>$v['openid']))->find();
			$order[$k]['uimg']=$uimg['headimgurl'];
		}
		$this->assign('order',$order);
		$this->display();
	}
	
	public function orderinfo(){
		$this->wxsdk();		//使用微信SDK
		//===================================================
		$orderid=I('orderid');
		if($orderid){
			$m=M('bb_order');
			$order=$m->where(array('orderid'=>$orderid))->find();
			$this->assign('order',$order);
			$order2=M('bb_order_yifu')->where(array('orderid'=>$orderid))->select();
			$this->assign('yifulist',$order2);
			$order3=M('bb_order_img')->where(array('orderid'=>$orderid))->select();
			$this->assign('yifuimg',$order3);
			$this->display();
		}
	}
	
	//抢单页面
	public function order_handle(){
		$orderid=I('orderid');
		$order=M('bb_order')->where(array('orderid'=>$orderid))->find();
		if($order['auntieid']){
			echo '<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">&nbsp;<link rel="stylesheet" href="/Public/client/css/cui.css" /><script type="text/javascript" src="/Public/client/js/jquery.js"></script><script src="/Public/business/js/normal.js?sj={$tj}"></script> <script> Msg("抢过了",2); </script>';
			exit;
		}
		$u=M('weixin_client_user')->field('headimgurl')->where(array('openid'=>$order['openid']))->find();
		$this->assign('order',$order);
		$this->assign('uimg',$u['headimgurl']);
		$this->display();
	}
	//服务页面
	public function order_fuwu(){
		$orderid=I('orderid');
		$order=M('bb_order')->where(array('orderid'=>$orderid))->find();
		$u=M('weixin_client_user')->field('headimgurl')->where(array('openid'=>$order['openid']))->find();
		//p($order);
		$this->assign('order',$order);
		$this->assign('uimg',$u['headimgurl']);
		$this->display();
	}
	//衣单页面
	public function yidan(){
		$this->wxsdk();		//使用微信SDK
		//===================================================
		$orderid=I('orderid');
		if($orderid){
			$order=M('bb_order')->where(array('orderid'=>$orderid))->find();
			//普洗袋洗==
			if($order['wash_type']==1){
				$product=M('bb_product')->select();
				$this->assign('product',$product);
			}
			//品牌精细==
			if($order['wash_type']==2){
				$product=M('bb_product_jx')->select();
				$this->assign('product',$product);
			}
			//洗鞋==
			if($order['wash_type']==3){
				$product=M('bb_product')->where(array('type'=>9))->select();
				$this->assign('product',$product);
				//p($product);
			}
			//===================================================
			$this->assign('orderid',$orderid);
			$this->assign('order',$order);
			$defect=M('bb_defect')->select();	$this->assign('defect',$defect);	//瑕疵
			$annex=M('bb_annex')->select();	$this->assign('annex',$annex);	//附件
		}
		$this->display();
	}
	//========
	public function addauntie(){
		$shopid=I('shopid');
		if($shopid){
			$cache=M('bb_auntie')->where(array('shopid'=>$shopid,'openid'=>self::$session['openid']))->find();
			$this->assign('cache',$cache);
			$this->assign('shopid',$shopid);
			$this->display();
		}else{
			$this->diemsg(0,'出错了！');
		}
	}
	
	//关于我们
	public function rules(){
		$this->display();
	}
	
	//我的提成
	public function deduct(){
		$id=self::$session['auntie_user']['id'];
		$cache=M('auntie_log')->where(array('auntieid'=>$id))->select();
		$this->assign('cache',$cache);
		
		$auntie=M('bb_auntie')->where(array('id'=>$id))->find();
		$this->assign('auntie',$auntie);
		
		$this->display();
	}
	//资料
	public function userinfn(){
		$this->display();
	}
	//修改姓名
	public function editname(){
		$this->display();
	}
	//修改手机号码
	public function editphone(){
		$this->display();
	}
	//修改地址
	public function editaddress(){
		$this->display();
	}
	public function editbank(){
		$this->display();
	}
	
	public function ljtx(){
		$this->display();
	}
	
	public function add_tx(){

		
		if(IS_POST){

			
			$p=I('post.');
			$p['addtime']=time();
			$p['openid']=$_SESSION['auntie']['openid'];
			
			
			if(!($_SESSION['auntie']['openid'])){
				$info['status']=0;
				$info['msg']='no！';
				$this->ajaxreturn($info);
				die;
			}
			
			$arr_f = M('bb_auntie')->where('openid = "'.$_SESSION['auntie']['openid'].'"')->find();

			if($arr_f['money'] < $p['money']){
				$info['status']=0;
				$info['msg']='no！';
				$this->ajaxreturn($info);
				die;
			}
			$arr_x['money'] = $arr_f['money']-$p['money'];
			M('bb_auntie')->where('id = '.$arr_f['id'])->save($arr_x);
			

			$p['order_num']="o".time();
			$re=M('bb_txorder')->add($p);
			if($re){
				
				$info['status']=1;
				$info['msg']='ok！';
				
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
	
	public function txcx(){
		$cache=M('bb_txorder')->where('openid = "'.$_SESSION['auntie']['openid'].'"')->select();
		$auntie=M('bb_auntie')->where('openid = "'.$_SESSION['auntie']['openid'].'"')->find();
		$this->assign('auntie',$auntie);
		
		foreach($cache as $k=>$v){
			if($v['status'] == 0){
				$cache[$k]['status'] ='等待中';
			}else{
				$cache[$k]['status'] ='已完成';
			}
			
			if($v['overtime'] == 0){
				$cache[$k]['overtime'] ='未付款';
			}else{
				$cache[$k]['overtime'] =date('Y-m-d',$cache[$k]['overtime']);
			}
			
			
			
			
		}
		
		$this->assign('cache',$cache);
		$this->display();
	}
	
	//=微信SDK============================
	public function wxsdk(){
		//分享js 参数------------------------------
		$options['appid']=C('BB_APPID_auntie');
		$options['secret']=C('BB_SECRET_auntie');
		$wx=new \bb\Wechat($options);	//new 微信类
		$rejsapi=$wx->getjsapi();
		$this->assign('jsapi',$rejsapi);
		//-----------------------------------
	}
	
	
	public function wdfx(){
		
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx_u=new \bb\Wechat($options);
		
		$re=$wx_u->getQRUrl_str(self::$session['auntie_user']['id'].'Auntie');
		
		$this->assign('tuigungimg',$re);
		
		
		$m=M('bb_auntie');
		$arr=array();
		
		//测试
		if(!self::$session['auntie_user']['wdxx']){
			self::$session['auntie_user']['wdxx'] ='6,9,33';
		}
		
		$xiaxian_1=explode(',',self::$session['auntie_user']['wdxx']);
		$xiaxian_1_num = count($xiaxian_1);
		$xiaxian_2= array();
		$xiaxian_3= array();
		
		foreach($xiaxian_1 as $k => $v){
			$map_fx['path'] = array('like','%,'.$v.'%');
			$fx=M('client_user')->where($map_fx)->select();
			
			foreach($fx as $kv=>$vv){
				$fx_a=explode(',',$vv['path']);
				foreach($fx_a as $ke => $ve){
					if($ve == $data[$k]['id'] ){
						switch ($ke)
						{
						case count($fx_a)-1:
						  $map_fx2[] = $vv['id'];
						  break;  
						case count($fx_a)-2:
						  $map_fx3[] = $vv['id'];
						  break;
						  			  
						default:
							
						}
						break;
					}
				}
			}
		}
		
			$xiaxian_2_num = count($xiaxian_2);
			$xiaxian_3_num = count($xiaxian_3);
			$xianxiannum = count($xiaxian_1_num)+count($xiaxian_2_num)+count($xiaxian_3_num);
		
		$this->assign('xianxiannum',$xianxiannum);
		
		
		$this->display();
	}
}